<?php
namespace Api\Event\Thirdservice;

use Common\Dto\BaseDto;

/*
 * appid 662016010 正式服
 * appid 662016005 测试服
 */

class ThirdserviceNgaEvent extends ThirdserviceEvent
{
    public function _initialize() {
        parent::_initialize();

        $this->vEvent = event('video');
    }

    public function share(BaseDto $dto) {
        if (!$dto->vid || !$dto->uid) {
            return false;
        }

        $user = service('sso')->where(['uid' => $dto->uid, 'channel' => 'stargame'])->find();

        $video = event('video')->info(intval($dto->vid));
        if (empty($user) || empty($video)) {
            return false;
        }

        $nga_domain = 'http://bbs.ngacn.cc';

        $url = $nga_domain.'/post.php';
        $content = $video['detail'];
        $content .= "[flash]".DOMAIN."/home/share/video/id/{$video['id']}[/flash]";

        $params = [
            'fid' => 528,
            'step' => 2,
            'lite' => 'js',
            'post_subject' => '[66PLAY]'.iconv('UTF-8', 'GBK', $video['title']),
            'post_content' => iconv('UTF-8', 'GBK', $content),
            'ngaPassportUid' => 'stargametoken0'.$user['open']['userId'],
            'ngaPassportCid' => $user['open']['access_token'],
        ];

        $header = [
            "content-type: application/x-www-form-urlencoded; charset=GBK"
        ];
        $response = http_request($url, $params, $header, 'post');

        $result = iconv('GBK', 'UTF-8', $response);

        $result_json = str_replace('window.script_muti_get_var_store=', '', $result);

        $res = json_decode($result_json, true);

        if (!isset($res['data']['__MESSAGE'][4]) || empty($res['data']['__MESSAGE'][4])) {
            return [
                'error' => $res['data']['__MESSAGE'][1],
                'url'   => ''
            ];
        }
        return [
            'error' => '',
            'url'   => $nga_domain . $res['data']['__MESSAGE'][4]
        ];
    }
}